<?php
/**
 * @author JohnnyDen <413819942@qq.com>
 * @package addons\autocontentincome\controller
 * @copyright 2018年11月22日
 */
namespace addons\autocontentincome\controller;

use mip\Mip;

class AutoContentIncomeCallBack extends Mip
{
    protected $beforeActionList = ['start'];
    protected $addonsName = '';
    public function start()
    {
        $this->item = $this->articles;
        $this->itemCategory = $this->articlesCategory;
        $this->itemContent = $this->articlesContent;
        $this->itemType = 'article';
    }
    
    public function record()
    {
        try{
            $data   = file_get_contents('php://input');
            $data   = json_decode($data, true);
            $res = \XppOpenApi\Util\Util::checkSign($data);
            if (!$res) {
                file_put_contents(RUNTIME_PATH.'aci_callback.log', "\n".'验证失败：'.json_encode($data), FILE_APPEND);
                return jsonError('验证失败');
            }
            
            $finished = $data['finished'];
            $aci_id = $data['attach'];
            
            if ($finished) {
                return $this->finished($aci_id);
            }
            
            $title = $data['title'];
            $keywords = $data['keywords'];
            $description = $data['description'];
            $content = $data['content'];
            $publish_time = time();
            
            $itemType = $this->itemType;
            $aci    = db('AutoContentIncome')->field('cid,id_org')->where('id', $aci_id)->find();
            if (!$aci) {
                file_put_contents(RUNTIME_PATH.'aci_callback.log', "\n".'数据迁入规则不存在：'.$aci_id, FILE_APPEND);
                return jsonError('数据迁入规则不存在');
            }
            db('AutoContentIncome')->where('id', $aci_id)->update(array(
                'exe_status'    => 'doing',
                'exe_end_time'  => time(),
                'exe_suc_count' => db()->raw('exe_suc_count+1'),
            ));
            
            if (!$title) {
                file_put_contents(RUNTIME_PATH.'aci_callback.log', "\n".'请输入标题：'.json_encode($data), FILE_APPEND);
                return jsonError('请输入标题');
            }
            if (!$content) {
                file_put_contents(RUNTIME_PATH.'aci_callback.log', "\n".'请输入内容：'.json_encode($data), FILE_APPEND);
                return jsonError('请输入内容');
            }
            $cid    = $aci['cid'];
            if (!$cid) {
                file_put_contents(RUNTIME_PATH.'aci_callback.log', "\n".'分类不存在：'.json_encode($aci), FILE_APPEND);
                return jsonError('分类不存在');
            }
            $uid    = db('Org')->where('id_org', $aci['id_org'])->value('master_uid');
            $itemInfo = db($this->item)->where('id_org', $aci['id_org'])->where('title',$title)->find();
            if ($itemInfo) {
                file_put_contents(RUNTIME_PATH.'aci_callback.log', "\n".'标题已存在', FILE_APPEND);
                return jsonError('标题已存在');
            }
            $uuid = uuid();
            $resArray = array (
                'id_org' => config('orgId'),
                'title' => htmlspecialchars($title),
                'keywords' => $keywords,
                'description' => $description,
                'uid' => $uid,
                'cid' => $cid,
                'publish_time' => $publish_time,
                'uuid' => $uuid,
                'content_id' => $uuid,
            );
            db($this->item)->insert($resArray);
            $itemInfo = db($this->item)->where('id_org', $aci['id_org'])->where('uuid',$uuid)->find();
            if ($itemInfo) {
                db($this->itemContent)->insert(array(
                    'id' => $uuid,
                    'content' => htmlspecialchars($content),
                ));
            }
            return jsonSuccess('发布成功');
            
        } catch(\Exception $e) {
            file_put_contents(RUNTIME_PATH.'aci_callback.log', "\n".$e->getMessage()."\n".json_encode($data), FILE_APPEND);
            return jsonError('发布失败');
        }
    }
    
    public function finished($aci_id) {
        if (0 == db('AutoContentIncome')->where('id', $aci_id)->count()) {
            file_put_contents(RUNTIME_PATH.'aci_callback.log', "\n".'数据迁入规则不存在：finished', FILE_APPEND);
            return jsonError('数据迁入规则不存在');
        }
        
        db('AutoContentIncome')->where('id', $aci_id)->update(array(
            'exe_status'    => 'default',
        ));
        
        return jsonSuccess('完成');
    }
    
}
